
<div class="row animate fadeIn">

    <form id="luForm" class="form-horizontal"  method="post">
        <?php foreach ($result as $r) {?>
        <input type="hidden" id="LookUpType" name="LookUpType" value="<?php echo $r->lookup_type; ?>">
        <div class="ibox-title" style="border:none;"><h5 style="color: #00afef"><i>Edit Look Up</i></h5>
            <div class="ibox-tools">
                <button id="put_lookup" type="button" class="btn btn-sm btn-primary save-btn-tab">Save</button>
                <!-- <a id="cancel_edit" class="btn btn-sm btn-outline btn-info cancel-btn-tab">Cancel</a> -->
            </div>
        </div>
        <div class="ibox-content">
            <div class="form-group"><label class="col-lg-3 control-label">Code</label>
                <div class="col-lg-10 input-s">
                    <input type="text" name = "LookUpCode" class="form-control" value="<?php echo $r->lookup_type; ?>" readonly>
                </div>
            </div>
            <div class="form-group"><label class="col-lg-3 control-label">Description</label>
                <div class="col-lg-11 input-s">
                    <input type="text" id="LookUpName" value="<?php echo $r->description; ?>" name="moduleName" class="form-control">
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-3 control-label">Enabled</label>
                <div class="col-lg-10 input-s">
                    <input type="checkbox" name="LookUpEnableFlag" value="ok" class="i-checks" <?php echo ($r->enable_flag == 'Y' ? 'checked' : ''); ?> >
                </div>
            </div>
            <?php }
?>
    </form>
          </div>
          <div class="ibox-title no-top-border">
            <button class="btn btn-success m-t-n-xs btn-md add-tab-btn" data-toggle="modal" data-target="#addNew">New</button>
          </div>
          <div class="ibox-content">
            <table class="table table-bordered">
                <thead>
                  <tr>
                    <th>Code</th>
                    <th>Meaning</th>
                    <th>Description</th>
                    <th>Enable Flag</th>
                    <th></th>
                  </tr>
                </thead>
                <tbody id="luv">
                  
                </tbody>
            </table>
        </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
           <h4 class="modal-title" id="myModalLabel">Edit Look Up Values</h4>
        </div>
      <div class="modal-body">
        <div class="row">
          <form id="luvForm" method="post" class="form-horizontal">
              <input type="hidden" name="LookUpType">
              <div class="form-group">
                  <label class="col-lg-3 control-label">Code</label>
                  <div class="col-lg-5">
                      <input type="text" name="LookUpCode" class="form-control" value="" readonly>
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-3 control-label">Meaning</label>
                  <div class="col-lg-5">
                      <input type="text" name="LookUpMeaning" class="form-control" value="">
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-3 control-label">Description</label>
                  <div class="col-lg-5">
                      <input type="text" name="LookUpDesc" class="form-control" value="">
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-3 control-label">Enable FLag</label>
                  <div class="col-lg-5">
                      <input type="checkbox" value="ok" name="LookUpEF" class="i-checks">
                  </div>
              </div>
          </form>
        </div>
      </div>
      <div class="modal-footer">
           <button id="put_lookupvalues" type="button" class="btn btn-primary" >Save changes</button>
      </div>
    </div>
  </div>
</div> <!-- end-of-modal -->

<div class="modal fade" id="addNew" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
           <h4 class="modal-title" id="myModalLabel">Edit Look Up Values</h4>
        </div>
      <div class="modal-body">
        <div class="row">
          <form id="luvFormAdd" method="post" class="form-horizontal">
              <input type="hidden" name="LookUpType" value="<?php echo $result[0]->lookup_type; ?>">
              <div class="form-group">
                  <label class="col-lg-3 control-label">Code</label>
                  <div class="col-lg-5">
                      <input type="text" name="LookUpCode" class="form-control" value="">
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-3 control-label">Meaning</label>
                  <div class="col-lg-5">
                      <input type="text" name="LookUpMeaning" class="form-control" value="">
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-3 control-label">Description</label>
                  <div class="col-lg-5">
                      <input type="text" name="LookUpDesc" class="form-control" value="">
                  </div>
              </div>
              <div class="form-group">
                  <label class="col-lg-3 control-label">Enable FLag</label>
                  <div class="col-lg-5">
                      <input type="checkbox" value="ok" checked name="LookUpEF" class="i-checks">
                  </div>
              </div>
          </form>
        </div>
      </div>
      <div class="modal-footer">
           <button id="store_lookupvalues" type="button" class="btn btn-primary" >Save</button>
      </div>
    </div>
  </div>
</div> <!-- end-of-modal -->

<script>
        $(document).ready(function () {
                $('.i-checks').iCheck({
                    checkboxClass: 'icheckbox_square-green',
                    radioClass: 'iradio_square-green',
                });
        $(document).on('click','.edit-luv',function(){
          var luv_type = $(this).closest('tr').children('.type').text();
          var luv_code = $(this).closest('tr').children('.code').text();
          var luv_meaning = $(this).closest('tr').children('.meaning').text();
          var luv_desc = $(this).closest('tr').children('.description').text();
          var luv_ef = $(this).closest('tr').children('.enable_flag').text();
          $('#luvForm input[name="LookUpCode"]').val(luv_code);
          $('#luvForm input[name="LookUpMeaning"]').val(luv_meaning);
          $('#luvForm input[name="LookUpDesc"]').val(luv_desc);
          if(luv_ef == 'Y'){
            $('#luvForm input[name="LookUpEF"]').attr('checked',true);
            $('#luvForm input[name="LookUpEF"]').iCheck('update');
          }else{
            $('#luvForm input[name="LookUpEF"]').removeAttr('checked');
            $('#luvForm input[name="LookUpEF"]').iCheck('update');
          }
          $('#luvForm input[name="LookUpType"]').val(luv_type);
          console.log(luv_ef);
          $('#myModal').modal('show');
        });

        getLookUpValues();
        function getLookUpValues(){
          var type = $('#LookUpType').val();
          $.ajax({
            type: "POST",
            url: "<?php echo base_url($this->session->userdata('forajax') . '/getLookUpValues'); ?>",
            data: {'lookuptype': type },
            dataType: 'json',
            success: function(data){
                var tmp = '';
                $.each(data, function(i,item) {
                  tmp += "<tr>" 
                      + "<td class='type' style='display: none;'>" + data[i].lookup_type + "</td>"
                      + "<td class='code'>" + data[i].lookup_code + "</td>"
                      + "<td class='meaning'>" + data[i].meaning + "</td>"
                      + "<td class='description'>" + data[i].description + "</td>"
                      + "<td class='enable_flag' style='display: none;'>" + data[i].enable_flag + "</td>"
                      + '<td align="center" class=""><div class="state icheckbox_square-green '+ (data[i].enable_flag == 'Y' ? 'checked' : 'normal') +'"></div></td>'
                      + "<td align='center'><button class='btn btn-sm btn-outline btn-info btn-xs edit-luv' type='button'><i class='fa fa-pencil fa-fw'></i></button></td>"
                      + "</tr>";
                });

                $('#luv').html(tmp);
            }
          });
        }

        $('#edit_menuForm').click(function(){
            if($("#menu_edit_form").valid()==true){
               var mid = $('#edit_id').val();
               var mname = $('#edit_name').val();
            dataString = $("#menu_edit_form").serialize();
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/edit_menu'); ?>",
                        data: dataString,
                        dataType: 'json',
                    success: function(data){
                            if(data.err == 'Y'){
                               var sd = '#'+mid;
                              $('#resultfound tr').find(sd).html(mname);
                              notifymsg("success",". . .Menu Updated");
                              $('#myModal').modal('hide');
                                }
                            }
                        });
                }
        });
        $("#luvFormAdd").validate({
             rules: {
                 LookUpCode: {
                     required: true
                 }
             }
        });

        $("#luForm").validate({
             rules: {
                 LookUpName: {
                     required: true
                 }
             }
        });

        $(document).on('click',"#put_lookup",function(){
          if($("#luForm").valid() == true){
            $.ajax({
              type: "POST",
              url: "<?php echo base_url($this->session->userdata('forajax') . '/updateLookUp'); ?>",
              data: $("#luForm").serialize(),
              dataType: 'json',
              success: function(data){
                notifymsg("success"," . . .Update Succesful");
                $("#put_lookup").removeAttr('disabled');
              },
              beforeSend: function (){
                $("#put_lookup").attr('disabled',true);
              }
            });
          }
        });

        $(document).on('click',"#store_lookupvalues",function(){
          if($("#luvFormAdd").valid() == true){
            $.ajax({
              type: "POST",
              url: "<?php echo base_url($this->session->userdata('forajax') . '/insertLookUpValues'); ?>",
              data: $("#luvFormAdd").serialize(),
              dataType: 'json',
              success: function(data){
                if(data.err == 1){
                  getLookUpValues();
                  $('#addNew').modal('hide');
                  notifymsg("danger",data.msg);
                  $("#store_lookupvalues").removeAttr('disabled');
                }else{
                  getLookUpValues();
                  $('#addNew').modal('hide');
                  notifymsg("success"," . . .Add Succesful");
                  $("#luvFormAdd")[0].reset();
                  $("#store_lookupvalues").removeAttr('disabled');
                }
              },
              beforeSend: function(){
                $("#store_lookupvalues").attr('disabled',true);
              }
            });
          }
        });

        $(document).on('click',"#put_lookupvalues",function(){
          if($("#luvForm").valid() == true){
            $.ajax({
              type: "POST",
              url: "<?php echo base_url($this->session->userdata('forajax') . '/updateLookUpValues'); ?>",
              data: $("#luvForm").serialize(),
              dataType: 'json',
              success: function(data){
                getLookUpValues();
                $('#myModal').modal('hide');
                $("#put_lookupvalues").removeAttr('disabled');
                notifymsg("success"," . . .Update Succesfull");
              },
              beforeSend: function(){
                $("#put_lookupvalues").attr('disabled',true);
              }
            });
          }
        });
               $('#cancel_edit').click(function () {
                var mCode_click =$('input[name=moduleCode]').val();
                var mName_click =$('#mod_name').val();
                var mDir_click  =$('input[name=moduleDir]').val();

                if(mCode != mCode_click || mName != mName_click || mDir != mDir_click){
                              swal({
                                          title: "Are you sure?",
                                          text: "Your work will not be save",
                                          type: "warning",
                                          showCancelButton: true,
                                          confirmButtonColor: "#DD6B55",
                                          confirmButtonText: "Leave",
                                          cancelButtonText: "Stay",
                                          closeOnConfirm: true,
                                          closeOnCancel: true },
                                      function (isConfirm) {
                                          if (isConfirm) {
                                            swal("success");
                                            close_edit(); active_search();

                                          } else {
                                              swal("Cancelled");
                                          }
                                      });
                }else{
                    close_edit(); active_search();
                }
                          });

                $("#e_module-menu-form").validate({
                rules:{
                  menu_name: {
                      required: true,
                      lettersonly: true
                  }
                },
                submitHandler: function(form){
                    var trHTML = '';
                $('#tbodydaw').empty();
                var modulename = $('#ipgName').val();
                var moduleid   = $('#mod_id').val();
                    $.ajax({
                        type: "POST",
                        url: "<?php echo base_url($this->session->userdata('forajax') . '/insert_module_menu'); ?>",
                        data: { "menu-name": modulename, "module-id": moduleid  },
                        dataType: 'json',
                        success: function(data){
                            notifymsg("success",". . .Menu Added Successfully");
                            $("#change3").load("<?php echo $this->session->userdata('forload') . '/edit_form/'; ?>"+moduleid)
                            edit_action('show'); search_action('close');
                        }
                      });
                 }
               });


                $('#editX').click(function(){
                    close_edit();
                });
            });
</script>

